package com.view.recyclerViewDivider;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.State;
import android.view.View;

/**
 * Created by Administrator on 2016/4/6.
 */
public class RecyclerViewGridLayoutDivider extends RecyclerView.ItemDecoration {
    private Drawable mDivider;

    public RecyclerViewGridLayoutDivider(Drawable mDivider) {
        this.mDivider = mDivider;
    }


    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, State state) {
        drawTopDivider(c, parent);
        drawBottomDivider(c, parent);
        drawLeftDivider(c, parent);
        drawRightDivider(c, parent);
    }

    //理解为画矩形,必须确定四个点
    //绘制顶部分割线
    private void drawTopDivider(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int top = child.getTop() - mDivider.getIntrinsicHeight();
            final int bottom = child.getTop();
            final int left = child.getLeft() - mDivider.getIntrinsicHeight();
            final int right = child.getRight() + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    //绘制底部分割线
    private void drawBottomDivider(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int top = child.getBottom();
            final int bottom = child.getBottom() + mDivider.getIntrinsicHeight();
            final int left = child.getLeft() - mDivider.getIntrinsicHeight();
            final int right = child.getRight() + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }


    //绘制左侧分割线
    private void drawLeftDivider(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int top = child.getTop();
            final int bottom = child.getBottom();
            final int left = child.getLeft() - mDivider.getIntrinsicWidth();
            final int right = child.getLeft();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);

        }
    }

    //绘制右侧分割线
    private void drawRightDivider(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int top = child.getTop();
            final int bottom = child.getBottom();
            final int left = child.getRight();
            final int right = child.getRight() + mDivider.getIntrinsicWidth();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
        int width = mDivider.getIntrinsicWidth();
        int height = mDivider.getIntrinsicHeight();
        outRect.set(width + 10, height + 10, width + 10,
                height + 10);
    }


}
